package licode.unisop.oidc.authsa.stp;

import cn.dev33.satoken.stp.SaLoginModel;
import cn.dev33.satoken.stp.StpUtil;
import licode.unisop.oidc.auth.modal.AuthorContext;
import licode.unisop.oidc.auth.modal.OidcToken;
import licode.unisop.oidc.authsa.service.SaSessionProxy;

/**
 * 内部认证工具类
 *
 * @author licode
 */
public class SaAuthUtil {
    private SaAuthUtil() {
    }

    /**
     * 获取 StpLogic 对象
     *
     * @return /
     */
    public static SaStpLogic getStpLogic() {
        return (SaStpLogic) StpUtil.getStpLogic();
    }

    public static SaSessionProxy querySessionByLoginId(String loginId) {
        return getStpLogic().querySessionByLoginId(loginId);
    }

    public static SaSessionProxy querySessionBySsoToken(String ssoToken) {
        return getStpLogic().querySessionBySsoToken(ssoToken);
    }

    public static SaSessionProxy querySessionByAccessToken(String accessToken) {
        return getStpLogic().querySessionByAccessToken(accessToken);
    }

    public static SaSessionProxy querySessionByRefreshToken(String refreshToken) {
        return getStpLogic().querySessionByRefreshToken(refreshToken);
    }

    public static SaSessionProxy login(String userId, SaLoginModel loginModel,
                                       SaTokenSign tokenSign, AuthorContext context) {
        return getStpLogic().login(userId, loginModel, tokenSign, context);
    }

    public static SaSessionProxy buildMasterSession(String loginId, SaLoginModel loginModel) {
        return getStpLogic().buildMasterSession(loginId, loginModel);
    }

    public static void loginSso(String loginId, SaLoginModel loginModel,
                                          SaTokenSign tokenSign, AuthorContext context) {
        getStpLogic().loginSso(loginId, loginModel, tokenSign, context);
    }

    public static void logoutByAccessToken(String accessToken) {
        getStpLogic().logoutByAccessToken(accessToken);
    }

    public static void logoutByAccessToken(String accessToken, SaSessionProxy session) {
        getStpLogic().logoutByAccessToken(accessToken, session);
    }

    public static void logoutBySso(String accessToken, SaSessionProxy session) {
        getStpLogic().logoutBySso(accessToken, session);
    }

    public static OidcToken refreshLogin(String refreshToken, SaSessionProxy session, boolean newToken) {
        return getStpLogic().refreshLogin(refreshToken, session, newToken);
    }

    /**
     * 通过账号ID检查对应会话上的登录是否超时
     *
     * @param loginId 账号ID
     */
    public static void checkLoginTimeoutByLoginId(String loginId) {
        SaSessionProxy session = querySessionByLoginId(loginId);
        getStpLogic().checkLoginTimeoutOnSession(session);
    }

    public static SaTokenSign buildOauthTokenSign(AuthorContext contextInfo) {
        return getStpLogic().buildOauthTokenSign(contextInfo);
    }

    public static OidcToken tokenInfoFromAccessToken(String accessToken, SaSessionProxy session) {
        return getStpLogic().tokenInfoFromAccessToken(accessToken, session);
    }

    public static AuthorContext loginContextFromAccessToken(String accessToken) {
        return getStpLogic().loginContextFromAccessToken(accessToken);
    }

    public static OidcToken tokenInfoFromRefreshToken(String refreshToken) {
        return getStpLogic().tokenInfoFromRefreshToken(refreshToken);
    }

    public static String loginIdFromRefreshToken(String refreshToken) {
        return getStpLogic().loginIdFromRefreshToken(refreshToken);
    }

    public static String loginIdFromAccessToken(String accessToken) {
        return getStpLogic().loginIdFromAccessToken(accessToken);
    }

    public static String accessTokenFromRefreshToken(String refreshToken) {
        return getStpLogic().accessTokenFromRefreshToken(refreshToken);
    }

    public static String refreshTokenFromAccessToken(String accessToken) {
        return getStpLogic().refreshTokenFromAccessToken(accessToken);
    }
}
